/**
 *
 */
package com.mallsite.modules.shop.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.sys.entity.Role;
import com.jeesite.modules.sys.entity.User;
import com.jeesite.modules.sys.service.RoleService;
import com.jeesite.modules.sys.service.UserService;
import com.jeesite.modules.sys.utils.UserUtils;
import com.mallsite.modules.shop.entity.SupplierUser;
import com.mallsite.modules.shop.service.SupplierUserService;

/**
 * <p>
 * TODO 描述
 * </p>
 *
 * @author ruyang
 * @since 2020年11月30日
 */
@Controller
@RequestMapping(value = "${adminPath}/shop/supplierUser")
public class SupplierUserController extends BaseController {

	@Autowired
	private SupplierUserService supplierUserService;
	
	@Autowired
	private UserService userService;
	
	@Autowired
	private RoleService roleService;

	@ModelAttribute
	public SupplierUser get(String userCode, boolean isNewRecord) {
		return supplierUserService.get(userCode, isNewRecord);
	}

	@RequiresPermissions("shop:supplierUser:view")
	@RequestMapping(value = "list")
	public String list(SupplierUser supplierUser, Model model) {
		model.addAttribute("supplierUser", supplierUser);
		return "modules/shop/supplierUserList";
	}

	@RequiresPermissions("shop:supplierUser:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<SupplierUser> listData(SupplierUser supplierUser, HttpServletRequest request, HttpServletResponse response) {
		supplierUser.setPage(new Page<>(request, response));
		Page<SupplierUser> page = supplierUserService.findPage(supplierUser);
		return page;
	}

	@RequiresPermissions("shop:supplierUser:view")
	@RequestMapping(value = "form")
	public String form(SupplierUser supplierUser, String op, Model model) {
		
		// 获取当前编辑供应商的角色和权限
		if (StringUtils.inString(op, Global.OP_AUTH)) {
			
			// 获取当前供应商所拥有的角色
			Role role = new Role();
			role.setUserCode(supplierUser.getUserCode());
			model.addAttribute("roleList", roleService.findListByUserCode(role));

		}
		
		// 操作类型：add: 全部； edit: 编辑； auth: 授权
		model.addAttribute("op", op);
		model.addAttribute("supplierUser", supplierUser);
		return "modules/shop/supplierUserForm";
	}

	@RequiresPermissions(value={"shop:supplierUser:edit","shop:supplierUser:authRole"}, logical=Logical.OR)
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated SupplierUser supplierUser, String op, HttpServletRequest request) {
		if (User.isSuperAdmin(supplierUser.getUserCode())) {
			return renderResult(Global.FALSE, "非法操作，不能够操作此供应商！");
		}
		if (!SupplierUser.USER_TYPE_SUPPLIER.equals(supplierUser.getUserType())){
			return renderResult(Global.FALSE, "非法操作，不能够操作此供应商！");
		}
		SupplierUser old = super.getWebDataBinderSource(request);
		if (!Global.TRUE.equals(userService.checkLoginCode(old != null ? old.getLoginCode() : "", supplierUser.getLoginCode()))) {
			return renderResult(Global.FALSE, text("保存供应商失败，登录账号''{0}''已存在", supplierUser.getLoginCode()));
		}
		Subject subject = UserUtils.getSubject();
		if (StringUtils.inString(op, Global.OP_ADD, Global.OP_EDIT) && subject.isPermitted("shop:supplierUser:edit")){
			supplierUserService.save(supplierUser);
		}
		if (StringUtils.inString(op, Global.OP_ADD, Global.OP_AUTH) && subject.isPermitted("shop:supplierUser:authRole")){
			userService.saveAuth(supplierUser);
		}
		return renderResult(Global.TRUE, text("保存供应商''{0}''成功", supplierUser.getUserName()));
	}
	
	/**
	 * 停用供应商
	 * @param supplierUser
	 * @return
	 */
	@RequiresPermissions("shop:supplierUser:updateStatus")
	@ResponseBody
	@RequestMapping(value = "disable")
	public String disable(SupplierUser supplierUser) {
		if (User.isSuperAdmin(supplierUser.getUserCode())) {
			return renderResult(Global.FALSE, "非法操作，不能够操作此供应商！");
		}
		if (!SupplierUser.USER_TYPE_SUPPLIER.equals(supplierUser.getUserType())){
			return renderResult(Global.FALSE, "非法操作，不能够操作此供应商！");
		}
		if (supplierUser.getCurrentUser().getUserCode().equals(supplierUser.getUserCode())) {
			return renderResult(Global.FALSE, text("停用供应商失败，不允许停用当前供应商"));
		}
		supplierUser.setStatus(User.STATUS_DISABLE);
		supplierUserService.updateStatus(supplierUser);
		return renderResult(Global.TRUE, text("停用供应商''{0}''成功", supplierUser.getUserName()));
	}
	
	/**
	 * 启用供应商
	 * @param supplierUser
	 * @return
	 */
	@RequiresPermissions("shop:supplierUser:updateStatus")
	@ResponseBody
	@RequestMapping(value = "enable")
	public String enable(SupplierUser supplierUser) {
		if (User.isSuperAdmin(supplierUser.getUserCode())) {
			return renderResult(Global.FALSE, "非法操作，不能够操作此供应商！");
		}
		if (!SupplierUser.USER_TYPE_SUPPLIER.equals(supplierUser.getUserType())){
			return renderResult(Global.FALSE, "非法操作，不能够操作此供应商！");
		}
		supplierUser.setStatus(User.STATUS_NORMAL);
		supplierUserService.updateStatus(supplierUser);
//		AuthorizingRealm.isValidCodeLogin(supplierUser.getLoginCode(), supplierUser.getCorpCode_(), null, "success");
		return renderResult(Global.TRUE, text("启用供应商''{0}''成功", supplierUser.getUserName()));
	}
	
	/**
	 * 密码重置
	 * @param supplierUser
	 * @return
	 */
	@RequiresPermissions("shop:supplierUser:resetpwd")
	@RequestMapping(value = "resetpwd")
	@ResponseBody
	public String resetpwd(SupplierUser supplierUser) {
		if (User.isSuperAdmin(supplierUser.getUserCode())) {
			return renderResult(Global.FALSE, "非法操作，不能够操作此供应商！");
		}
		if (!SupplierUser.USER_TYPE_SUPPLIER.equals(supplierUser.getUserType())){
			return renderResult(Global.FALSE, "非法操作，不能够操作此供应商！");
		}
		userService.updatePassword(supplierUser.getUserCode(), null);
//		AuthorizingRealm.isValidCodeLogin(supplierUser.getLoginCode(), supplierUser.getCorpCode_(), null, "success");
		return renderResult(Global.TRUE, text("重置供应商''{0}''密码成功", supplierUser.getUserName()));
	}

	/**
	 * 删除供应商
	 * @param supplierUser
	 * @return
	 */
	@RequiresPermissions("shop:supplierUser:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(SupplierUser supplierUser) {
		if (User.isSuperAdmin(supplierUser.getUserCode())) {
			return renderResult(Global.FALSE, "非法操作，不能够操作此供应商！");
		}
		if (!SupplierUser.USER_TYPE_SUPPLIER.equals(supplierUser.getUserType())){
			return renderResult(Global.FALSE, "非法操作，不能够操作此供应商！");
		}
		if (supplierUser.getCurrentUser().getUserCode().equals(supplierUser.getUserCode())) {
			return renderResult(Global.FALSE, text("删除供应商失败，不允许删除当前供应商"));
		}
		supplierUserService.delete(supplierUser);
		return renderResult(Global.TRUE, text("删除供应商'{0}'成功", supplierUser.getUserName()));
	}
}
